package com.runen.infinitemovement.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.ActivitiesEntity;
import com.runen.infinitemovement.tool.AnimationUtils;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.TimeUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class MyActivityAdapter extends BaseQuickAdapter<ActivitiesEntity, BaseViewHolder> implements LoadMoreModule {
    private Context context;
    private boolean isSelet;

    public MyActivityAdapter(Context context) {
        super(R.layout.item_activity);
        this.context = context;
    }

    @SuppressLint("NewApi")
    @Override
    protected void convert(@NonNull BaseViewHolder helper, ActivitiesEntity item) {
        RoundedCornersImage rci_cover = helper.getView(R.id.rci_cover);
        RelativeLayout rl_sports_data = helper.getView(R.id.rl_sports_data);
        LinearLayout ll_sports_data = helper.getView(R.id.ll_sports_data);
        ImageView iv_more = helper.getView(R.id.iv_more);
        ImageView iv_flag = helper.getView(R.id.iv_flag);
        if (item.flag == 1) {
            iv_flag.setVisibility(View.VISIBLE);
        } else {
            iv_flag.setVisibility(View.GONE);
        }
        AnimationUtils.setRotateAnimation(iv_more, 180, 0);
        rl_sports_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelet = !isSelet;
                if (isSelet) {
                    AnimationUtils.setRotateAnimation(iv_more, 0, 180);
                    ll_sports_data.setVisibility(View.VISIBLE);
                } else {
                    AnimationUtils.setRotateAnimation(iv_more, 180, 0);
                    ll_sports_data.setVisibility(View.GONE);
                }

            }
        });
        RelativeLayout rl_progress = helper.getView(R.id.rl_progress);
        TextView tv_label = helper.getView(R.id.tv_label);
        TextView tv_from_the_end_of_the = helper.getView(R.id.tv_from_the_end_of_the);
        TextView tv_title_label = helper.getView(R.id.tv_title_label);
        TextView tv_go_name = helper.getView(R.id.tv_go_name);
        ProgressBar progressBar = helper.getView(R.id.progressBar);
        TextView tv_target_time = helper.getView(R.id.tv_target_time);
        TextView tv_sports_num = helper.getView(R.id.tv_sports_num);
        TextView tv_ranking_num = helper.getView(R.id.tv_ranking_num);
        TextView tv_road_award = helper.getView(R.id.tv_road_award);
        if (!TextUtils.isEmpty(item.cover)) {
            GlideUtils.loadImage(context, item.cover, rci_cover);
        } else {
            GlideUtils.loadImage(context, CommonUtil.getColor(R.color.color_FF999999), rci_cover);
        }
        if (!TextUtils.isEmpty(item.startPoint) || !TextUtils.isEmpty(item.endPoint)) {
            tv_go_name.setText(item.startPoint + "-" + item.endPoint);
            tv_title_label.setText(item.startPoint + "-" + item.endPoint);
        } else {
            tv_go_name.setText(item.activeName);
            tv_title_label.setText(item.activeName);
        }

        //  活动状态 [0-未开始 1-进行中 2-已结束]
        if (item.activeTimeState == 0) {
            tv_label.setVisibility(View.VISIBLE);
            tv_label.setBackground(CommonUtil.getDrawable(R.drawable.bg_dbdbdb_fafafa_dp10));
            tv_label.setText("未开始");
            String s_e = "目标未开始(%s-%s)";
            String activeBeginTime = TimeUtil.dealDateFormat2(item.activeBeginTime);
            String activeDeadlineTime = TimeUtil.dealDateFormat2(item.activeDeadlineTime);
            tv_target_time.setText(String.format(s_e, activeBeginTime, activeDeadlineTime));
        } else if (item.activeTimeState == 1) {
            tv_label.setVisibility(View.VISIBLE);
            tv_label.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffd714_ff8d00));
            tv_label.setText("正在进行");
            String s_e = "目标进行中(%s-%s)";
            String activeBeginTime = TimeUtil.dealDateFormat2(item.activeBeginTime);
            String activeDeadlineTime = TimeUtil.dealDateFormat2(item.activeDeadlineTime);
            tv_target_time.setText(String.format(s_e, activeBeginTime, activeDeadlineTime));
        } else if (item.activeTimeState == 2) {
            tv_label.setVisibility(View.VISIBLE);
            tv_label.setBackground(CommonUtil.getDrawable(R.drawable.bg_dbdbdb_fafafa_dp10));
            tv_label.setText("已完成");
            String s_e = "目标已完成(%s-%s)";
            String activeBeginTime = TimeUtil.dealDateFormat2(item.activeBeginTime);
            String activeDeadlineTime = TimeUtil.dealDateFormat2(item.activeDeadlineTime);
            tv_target_time.setText(String.format(s_e, activeBeginTime, activeDeadlineTime));
        }

        if (item.activeBeginTime != null && item.activeDeadlineTime != null) {
            long now = System.currentTimeMillis();
            long beginTime = TimeUtil.getDateTimeToLong(TimeUtil.dealDateFormat1(item.activeBeginTime));
            long endTime = TimeUtil.getDateTimeToLong(TimeUtil.dealDateFormat1(item.activeDeadlineTime));
            String days = "";
            if (endTime > now) {
                days = TimeUtil.formatDay(endTime - now);
            }
            String str_end = "距活动结束还有%s天";
            tv_from_the_end_of_the.setText(String.format(str_end, days));
        }

        if (item.rate == 0) {
            rl_progress.setVisibility(View.GONE);
        } else {
            rl_progress.setVisibility(View.VISIBLE);
        }
        progressBar.setProgress((int) item.rate);
        tv_sports_num.setText(item.times + "次");
        tv_ranking_num.setText("第" + item.rank + "名");
        tv_road_award.setText(item.prize);
    }
}
